//
//  BaseViewController.m
//  CpicPeriodicaliPad
//
//  Created by magicmac on 12-9-3.
//  Copyright (c) 2012年 magicpoint. All rights reserved.
//

#import "BaseVC.h"
@interface BaseVC ()

@end

@implementation BaseVC

- (void)viewDidLoad{
    
    notiSendToUIKey = [NSString stringWithFormat:@"%@-%@",kNSendToUI,NSStringFromClass(self.class)];
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:notiSendToUIKey object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(senderFormBack:)
                                                name:notiSendToUIKey
                                              object:nil];
    [self createViews];
    [self createData];
    [self createEven];
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self fillViewsOnResume];
    [self fillDataOnResume];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:notiSendToUIKey object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(senderFormBack:)
                                                name:notiSendToUIKey
                                              object:nil];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:notiSendToUIKey object:nil];
}
//发送消息给后台
- (void)sendToHttp:(ThreadMessage *)threadMessage{
    threadMessage.returnUINotiKey = notiSendToUIKey;
    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:threadMessage,kNKeyThreadMessage, nil];
    //使用异步的方式发布广播
    [[NSNotificationQueue defaultQueue]enqueueNotification:[NSNotification notificationWithName:kNSendToHttp object:self userInfo:dict] postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnSender forModes:nil];
}

//从httpservice里获取的通知在这里统一处理
- (void)senderFormBack:( NSNotification *)notification{
    NSDictionary *dic = [notification userInfo];
    
    ThreadMessage *threadMessage = [dic valueForKey:kNKeyThreadMessage];
    //如果写了block则执行
    if (threadMessage.receiveUIBlock) {
        threadMessage.receiveUIBlock(threadMessage);
        return;
    }
    
    //如果异常直接处理异常
    if (threadMessage.httpError) {
        BOOL operationBOOL = [self returnError:threadMessage];
        if (!operationBOOL) {
            [self hideProgressView];
            [self alertMsg:kWHttpError];
        }
        return;
    }
    
    //    CLog(@"senderFormBack=%@",threadMessage);
    SEL returnUIMethod = [threadMessage returnUIMethod];
    if (returnUIMethod && [self respondsToSelector:returnUIMethod]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:returnUIMethod withObject:threadMessage];
#pragma clang diagnostic pop
    }else{
        [self returnDefault:threadMessage];
    }
}


//工厂方法创建 通过这个方法来控制入口，方便以后的扩展和更新

+(id)createVC:(Class )class1{
    return [[class1 alloc]init];
}
+(id)createVC
{
    return [self createVC:[self class]];
}
+(id)createNibVC:(Class )class1{
    NSString *className = NSStringFromClass(class1);
    id result =  [[class1 alloc]initWithNibName:className bundle:nil];
    return result;
}
+(id)createNibVC{
    return [self createVC:[self class]];
}

#pragma mark 子类继承的方法
-(void)createViews{
    
}
- (void)fillViewsOnResume{
    
}

- (void)createData{
    
}
- (void)fillDataOnResume{
}
-(void)createEven{
    
}
//返回默认的方法
- (void)returnDefault:(ThreadMessage *)threadMessage{
}
/**
 *  返回错误
 *
 *  @param threadMessage <#threadMessage description#>
 *
 *  @return 如果返回的YES,则自行处理,否则Base会处理
 */
- (BOOL)returnError:(ThreadMessage *)threadMessage{
    return NO;
}

#pragma mark 其他
-(void)pushToNextClass:(Class )mClass{
    id nextVC = [BaseVC createNibVC:mClass];
    if ([nextVC isKindOfClass:[UIViewController class]]) {
        [self pushToNextVC:nextVC];
    }
}
-(BOOL)checkThreadMessageAndAlertError:(ThreadMessage *)thMsg{
    
    if (thMsg.receiveIsSuccess) {
        return YES;
    }
    //    CLog(@"%@",thMsg.receiveJsonContent);
    [self alertMsg:thMsg.receiveMsg];
    return NO;
}

@end
